package top.arryweb.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.arryweb.application.common.constant.KeyName;
import top.arryweb.application.common.exception.BusinessRuntimeException;
import top.arryweb.application.domain.dto.CreateDistributionOrderDTO;
import top.arryweb.application.domain.pojo.BookKeepDistributionOrder;
import top.arryweb.application.domain.pojo.BookKeepPaymentOrder;
import top.arryweb.application.mapper.BookKeepDistributionOrderMapper;
import top.arryweb.application.service.IBookKeepDistributionOrderService;

/**
 * <p>
 * 分账订单 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-05-27
 */
@Service
public class BookKeepDistributionOrderServiceImpl extends ServiceImpl<BookKeepDistributionOrderMapper, BookKeepDistributionOrder> implements IBookKeepDistributionOrderService {

  @Autowired
  private RedisTemplate<String, Object> redisTemplate;

  @Autowired
  private BookKeepDistributionOrderMapper distributionOrderMapper;

  /**
   * 获取分账订单ID
   * @param dto
   * @return
   */
  @Override
  public Long getDistributionOrderId(CreateDistributionOrderDTO dto) {
    HashOperations<String, Long, BookKeepPaymentOrder> orderOperations = redisTemplate.opsForHash();
    BookKeepPaymentOrder paymentOrder = orderOperations.get(KeyName.ENTITY_ORDER.getName(), dto.getPaymentOrderId());
    if (Objects.isNull(paymentOrder)) {
      throw new BusinessRuntimeException("所属订单不存在");
    }

    /*BookKeepDistributionOrder distributionOrder = new BookKeepDistributionOrder();
    QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(true,distributionOrder.getPaymentOrderId(),dto.getPaymentOrderId())

    BigDecimal distributionOrderSum = null;
    if (distributionOrder.getPaymentOrderId().equals(dto.getPaymentOrderId()) && distributionOrder.getDeleteFlag() == false) {
      distributionOrderSum = distributionOrderMapper.selectCount(distributionOrder);
    }else {
      new BigDecimal("0.00");
    }
    if ((distributionOrderSum.compareTo(paymentOrder.getActualAmountPaid())) > 0) {
      throw new BusinessRuntimeException("不可再创建多余的分账订单");
    }*/
    return null;
  }
}
